#include "alibabasendphoneclient.h"
#include <QProcess>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>

AlibabaSendPhoneClient::AlibabaSendPhoneClient(QObject *parent)
    : QObject{parent}
{
    accessKeyId = "LTAI5tDFsUpwuWadYm3BWUN9";
    accessKeySecret = "v2QvL1OU5vU3otMAsLoPjLQGNOe99R";
    signName = "我的尚医通在线项目网站";
    templateCode = "验证码短信-副本";
}

void AlibabaSendPhoneClient::sendPhone(const QString &phoneNumber, const QString &message)
{
    QProcess process;
    QStringList arguments;
    arguments << "aliyun-python-sdk-dysmsapi"
              << "--access-key-id" << accessKeyId
              << "--access-key-secret" << accessKeySecret
              << "--phone-number" << phoneNumber
              << "--sign-name" << signName
              << "--template-code" << templateCode
              << "--template-param" << QString("{\"code\":\"%1\"}").arg(message);

    process.start("python", arguments);
    process.waitForFinished();

    QString result = process.readAllStandardOutput();
    QJsonDocument jsonResponse = QJsonDocument::fromJson(result.toUtf8());
    QJsonObject jsonObject = jsonResponse.object();

    if (jsonObject["Code"].toString() == "OK") {
        emit phoneSent(true);
    } else {
        emit phoneSent(false);
        qDebug() << "Failed to send SMS:" << jsonObject["Message"].toString();
    }
}
